package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    /**
     * 提交预约
     * @param map
     * @return
     */
    @Override
    public Result submitOrder(Map map) throws Exception{
        //1.体检机构是否支持该天预约(t_ordersetting表中是否存在对应日期)
        String orderDate = (String) map.get("orderDate");
        //将string -> 日期
        Date date = DateUtils.parseString2Date(orderDate);
        //根据[日期]查询[预约设置信息]
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null){
            //不支持该天预约
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.该天名额是否约满(t_ordersetting的reservations)
        //已预约名额
        int reservations = orderSetting.getReservations();
        //总共可预约名额
        int number = orderSetting.getNumber();
        if (number <= reservations){
            //已预约满
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //3.检测一个[用户(电话号码)]同—天是否预约了[相同套餐],[预约相同时间]的,不允许重复预约
        String telephone = (String) map.get("telephone");//[用户(电话号码)]
        //当前[电话]对应的会员信息
        Member member = memberDao.findByTelephone(telephone);
        if (member != null){//不为空:之前预约过,才可能存在重复情况
            Integer memberId = member.getId();

//            Integer setmealId = (Integer) map.get("setmealId");//[相同套餐]
            int setmealId = Integer.parseInt(map.get("setmealId").toString());//[相同套餐]
//            orderDate = (Date) map.get("orderDate");//[预约相同时间]
            Order order = new Order(memberId, date, null, Order.ORDERSTATUS_NO, setmealId);

            Order orderBySelect = orderDao.findByCondition(order);
            if (orderBySelect != null){
                //已有重复预约的情况
                //->预约失败
                return new Result(false,MessageConstant.HAS_ORDERED);
            }else {
                //可成功预约
                orderSetting.setReservations(reservations + 1);
                orderSettingDao.updateByOrderDate(orderSetting);
            }
        }else {
            //4.用户预约体检,但非机构会员→→直接预约成功,自动注册成为会员(通过手机号＋验证码完成登录)
            member = new Member();
            member.setIdCard(map.get("idCard").toString());
            member.setName(map.get("name").toString());
            member.setPhoneNumber(map.get("telephone").toString());
            member.setRegTime(new Date());
            member.setSex(map.get("sex").toString());

            //添加[member]到数据表
            memberDao.add(member);
        }
        //5.进行预约设置，添加预约纪录(t_order);更改预约人数(_ordersetting)
        //->添加预约纪录(t_order)
        Order order = new Order(member.getId(),date,map.get("orderType").toString(),Order.ORDERSTATUS_NO,Integer.parseInt(map.get("setmealId").toString()));
        //向数据库中添加[t_order]预约条目
        orderDao.add(order);
        int i = order.getId();
        //->更改预约人数(_ordersetting)
        orderSetting.setReservations(reservations + 1);
        orderSettingDao.updateByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());

//        String a = map.get("setmealId").toString();
//        Integer i = Integer.parseInt(map.get("setmealId").toString());

//        return null;
    }

    @Override
    public Map findById(Integer orderId) throws Exception {
        /*
        <p>体检人：{{orderInfo.member}}</p>
        <p>体检套餐：{{orderInfo.setmeal}}</p>
        <p>体检日期：{{orderInfo.orderDate}}</p>
        <p>预约类型：{{orderInfo.orderType}}</p>*/
        Map map = orderDao.findById(orderId);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
